#include "mainwindow.h"
#include "ui_mainwindow.h"


// 升级
bool MainWindow::Port_f316_Upgrade_File()
{
    //升级文件
    QString VerFileName = FilePath + "/" + VersF3xName;
    QFile VerFile(VerFileName);

    ui->progressBar->setValue(0);
    char LineBuff[512];
    qint64 Length;
    int VerSize = VerFile.size();
    FileSize_f316 = VerSize;

    if(0 >= FileSize_f316)
    {
        if(LangureStu == 1)
        {
            LangureStr = "No upgrade file";
        }
        else
        {
            LangureStr = "无升级文件";
        }
        InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
        return true;
    }
    int FileSize = 0;
    bool DoneStu = true;
    PageCnt_f316 = 0;

    QByteArray LineStr;
    LineStr.clear();
    Port_f316_Send_Data(LineStr,SEND_FILE_SIZE);
    Port_f316_Recv_Data_WaitOut(TIME_MCU_TIME_OUT);

    DoneStu = true;
    if(!VerFile.open(QIODevice::ReadOnly))
    {
        VerFile.close();
    }
    else if(PORT_STU_NULL != PortStu_f316)
    {
        while(!VerFile.atEnd())
        {
            if(VerSize - FileSize > 512)
            {
                Length = VerFile.read(LineBuff, 512);
            }
            else
            {
                Length = VerSize - FileSize;
                Length = VerFile.read(LineBuff, Length);
            }
            QByteArray VersBuff;
            VersBuff.clear();
            for(int i=0;i<Length;i++)
            {
                VersBuff.append(LineBuff[i]);
            }
            if(0 != Length)
            {
                Port_f316_Send_Data_Times(VersBuff, PORT_STU_UPG, SEND_DATA_UPG);
                if(PORT_STU_UPG != PortStu_f316)
                {
                    ui->pb_f316->setValue(0);
                    if(PORT_STU_BUSY == PortStu_f316)
                    {
                        if(LangureStu == 1)
                        {
                            LangureStr = "The device is busy, please operate later!";
                        }
                        else
                        {
                            LangureStr = "设备繁忙，请稍后操作!";
                        }
                        InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
                    }
                    else if(PORT_STU_TIMEOUT == PortStu_f316)
                    {
                        if(LangureStu == 1)
                        {
                            LangureStr = "The device timed out. Please check the device connection status and try again!";
                        }
                        else
                        {
                            LangureStr = "设备超时，请检查设备连接状态后重试!";
                        }
                        InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
                    }
                    else
                    {
                        if(LangureStu == 1)
                        {
                            LangureStr = "Upgrade failed!";
                        }
                        else
                        {
                            LangureStr = "升级失败!";
                        }
                        InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_FAILED);
                    }
                    DoneStu = false;
                    VerFile.close();
                    return false;
                }
            }
            FileSize += Length;
            ui->pb_f316->setValue((int)(90*FileSize/FileSize_f316));
        }
        VerFile.close();

        if(DoneStu == true)
        {
            QByteArray DoneBuff;
            DoneBuff.clear();
            Port_f316_Send_Data_Times(DoneBuff, PORT_STU_UPG, SEND_DATA_DONE);
            if(PORT_STU_ERROR == PortStu_f316)
            {
                return false;
            }
        }
    }
    if(PORT_STU_NULL == PortStu_f316)
    {
        return false;
    }
    else
    {
        ui->pb_f316->setValue(100);

        if(LangureStu == 1)
        {
            LangureStr = "Upgrade data transmission completed\n"
                         "\tPlease disconnect the device and reconnect for device upgrade\n"
                         "\tWait for the equipment upgrade to complete";
        }
        else
        {
            LangureStr = "升级数据传输完成\n"
                         "\t请断开设备，并重新连接进行设备升级\n"
                         "\t等待设备升级完成";
        }
        InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_SUCCESS);

        PortStu_f316 = PORT_STU_FREE;
    }

    return true;
}

// 读参数
bool MainWindow::Port_f316_Read_Param()
{
    //发送读参数命令
    QByteArray LineStr;
    LineStr.clear();
    Port_f316_Send_Data_Times(LineStr, PORT_STU_READ, SEND_READ_DATA_F316);
    if(PORT_STU_READ != PortStu_f316)
    {
        if(PORT_STU_BUSY == PortStu_f316)
        {
            if(LangureStu == 1)
            {
                LangureStr = "The device is busy, please operate later!";
            }
            else
            {
                LangureStr = "设备繁忙，请稍后操作!";
            }
            InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        else if(PORT_STU_TIMEOUT == PortStu_f316)
        {
            if(LangureStu == 1)
            {
                LangureStr = "The device timed out. Please check the device connection status and try again!";
            }
            else
            {
                LangureStr = "设备超时，请检查设备连接状态后重试!";
            }
            InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        else
        {
            if(LangureStu == 1)
            {
                LangureStr = "Failed to read Param!";
            }
            else
            {
                LangureStr = "参数读取失败!";
            }
            InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        return false;
    }
    if(!RecvData_f316.isEmpty())
    {
        double BatCurrent = 0;
        int BatTime = 0;
        // 充电电流
        BatCurrent = (((RecvData_f316[0] << 8)&0xFF00) + (RecvData_f316[1]&0x00FF)) * 1.0 / 100;
        ui->ds_cc_f316->setValue(BatCurrent);
        // 充电时间
        BatTime = ((RecvData_f316[2] << 8)&0xFF00) + (RecvData_f316[3]&0x00FF);
        ui->sb_cctime_f316->setValue(BatTime);
        // 放电电流
        BatCurrent = (((RecvData_f316[4] << 8)&0xFF00) + (RecvData_f316[5]&0x00FF)) * 1.0 / 100;
        ui->ds_dc_f316->setValue(BatCurrent);
        // 放电时间
        BatTime = ((RecvData_f316[6] << 8)&0xFF00) + (RecvData_f316[7]&0x00FF);
        ui->sb_dctime_f316->setValue(BatTime);

        if(LangureStu == 1)
        {
            LangureStr = "Param reading is completed!";
        }
        else
        {
            LangureStr = "参数读取完成!";
        }
        InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
        return true;
    }
    else
    {
        if(LangureStu == 1)
        {
            LangureStr = "Failed to read Param!";
        }
        else
        {
            LangureStr = "参数读取失败!";
        }
        InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        return false;
    }
}

// 写参数
bool MainWindow::Port_f316_Write_Param()
{
    //发送写参数命令
    QByteArray LineStr;
    LineStr.clear();
    int BatCurrent = 0;
    int BatTime = 0;
    // 充电电流
    BatCurrent = ui->ds_cc_f316->value()*100;
    LineStr.append((BatCurrent >> 8) & 0xFF);
    LineStr.append((BatCurrent >> 0) & 0xFF);
    // 充电时间
    BatTime = ui->sb_cctime_f316->value();
    LineStr.append((BatTime >> 8) & 0xFF);
    LineStr.append((BatTime >> 0) & 0xFF);
    // 放电电流
    BatCurrent = ui->ds_dc_f316->value()*100;
    LineStr.append((BatCurrent >> 8) & 0xFF);
    LineStr.append((BatCurrent >> 0) & 0xFF);
    // 放电时间
    BatTime = ui->sb_dctime_f316->value();
    LineStr.append((BatTime >> 8) & 0xFF);
    LineStr.append((BatTime >> 0) & 0xFF);
    //
    Port_f316_Send_Data_Times(LineStr, PORT_STU_BURN, SEND_BURN_DATA_F316);
    if(PORT_STU_BURN != PortStu_f316)
    {
        if(PORT_STU_BUSY == PortStu_f316)
        {
            if(LangureStu == 1)
            {
                LangureStr = "The device is busy, please operate later!";
            }
            else
            {
                LangureStr = "设备繁忙，请稍后操作!";
            }
            InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        else if(PORT_STU_TIMEOUT == PortStu_f316)
        {
            if(LangureStu == 1)
            {
                LangureStr = "The device timed out. Please check the device connection status and try again!";
            }
            else
            {
                LangureStr = "设备超时，请检查设备连接状态后重试!";
            }
            InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        else
        {
            if(LangureStu == 1)
            {
                LangureStr = "Failed to read Param!";
            }
            else
            {
                LangureStr = "参数写入失败!";
            }
            InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        return false;
    }
    if(LangureStu == 1)
    {
        LangureStr = "Param writeing is completed!";
    }
    else
    {
        LangureStr = "参数写入完成!";
    }
    InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
    return true;
}

// 初始化端口及定时器
void MainWindow::Port_f316_Port_Init()
{
    //
    Port_f316 = new QSerialPort();
    Thread_f316 = new QThread();
    connect(Thread_f316, &QThread::finished, this,&QObject::deleteLater);
    Thread_f316->start();
    Port_f316->moveToThread(Thread_f316);
    connect(Port_f316, &QSerialPort::readyRead, this, &MainWindow::Port_f316_Read_clicked);

    // b316
    SyncTimer_f316 = new QTimer;
    connect(SyncTimer_f316,&QTimer::timeout,this,&MainWindow::Port_f316_Sync_Timer);//同步
    SyncTimer_f316->stop();
    RecvTimer_f316 = new QTimer;
    connect(RecvTimer_f316,&QTimer::timeout,this,&MainWindow::Port_f316_Read_Data);//
    RecvTimer_f316->stop();
    WaitTimer_f316 = new QTimer(this);
    connect(WaitTimer_f316,&QTimer::timeout,this,&MainWindow::Port_f316_Once_Timer);
    WaitTimer_f316->setSingleShot(true);
}

//等待 同步 N ms 内mcu应答
void MainWindow::Port_f316_Recv_Data_Answer(int WaitStu)
{
    WaitTimer_f316->start(TIMER_SYNC_TIME_OUT);
    while(WaitStu == PortStu_f316) QApplication::processEvents();
    WaitTimer_f316->stop();
}
//发送命令超时
bool MainWindow::Port_f316_Send_Data_Times(QByteArray SendBuff, int LastStu, int SendStu)
{
    do
    {
        Port_f316_Send_Data(SendBuff, SendStu);

        LastPortStu_f316 = LastStu;
        PortStu_f316 = PORT_STU_WAIT;
        Port_f316_Recv_Data_Answer(PORT_STU_WAIT);
        if(PORT_STU_TIMEOUT == PortStu_f316)
        {
            RecvData_f316.clear();
            SendCnt_f316++;
            if(PageCnt_f316)
            {
                PageCnt_f316--;
            }
        }
        else if(LastPortStu_f316 == PortStu_f316)
        {
            SendCnt_f316 = 0;
        }
        if(SendCnt_f316 == 10 || PORT_STU_ERROR == PortStu_f316)
        {
            SendCnt_f316 = 0;
            return false;
        }
    }while(SendCnt_f316);

    return true;
}
//等待 N ms 用来等待mcu超时
void MainWindow::Port_f316_Recv_Data_WaitOut(int TimeVal)
{
    QElapsedTimer WaitTime;
    WaitTime.start();
    while(!WaitTime.hasExpired(TimeVal))QApplication::processEvents();
    RecvData_f316.clear();
}
//命令发送
void MainWindow::Port_f316_Send_Data(QByteArray LineStr,int SendStu)
{
    QByteArray LineBuff = LineStr;
    QByteArray SendBuff;
    SendBuff.resize(PORT_SIZE_MAX_F210);
    int BuffCnt = 0;

    QByteArray RandNums;
    RandNums.resize(4);
    RandNums.clear();
    generateRandomNumber(RandNums, 4);
    for(int icnt=0;icnt<4;icnt++)
    {
        SendBuff[icnt] = RandNums.at(icnt);
    }
    BuffCnt += 4;
    int TempModel;
    TempModel = ModelStu;
    //id
    switch (SendStu) {
    case SEND_DATA_SYNC:
    {
        SendBuff[BuffCnt] = 0xBB;
        SendBuff[BuffCnt+1] = 0x00;
        SendBuff[BuffCnt+2] = 0x00;
        SendBuff[BuffCnt+3] = 0x01;
        break;
    }
    case SEND_FILE_SIZE:
    {
        SendBuff[BuffCnt] = 0xC5;
        SendBuff[BuffCnt+1] = ((FileSize_f316>>16)&0x00FF);
        SendBuff[BuffCnt+2] = ((FileSize_f316>>8)&0x00FF);
        SendBuff[BuffCnt+3] = (FileSize_f316&0x00FF);
        break;
    }
    case SEND_DATA_UPG:
    {
        PageCnt_f316++;
        SendBuff[BuffCnt] = 0xC6;
        SendBuff[BuffCnt+1] = ((PageCnt_f316>>8)&0x00FF);
        SendBuff[BuffCnt+2] = (PageCnt_f316&0x00FF);
        SendBuff[BuffCnt+3] = 0x00;
        break;
    }
    case SEND_DATA_DONE:
    {
        SendBuff[BuffCnt] = 0xC6;
        SendBuff[BuffCnt+1] = 0xFF;
        SendBuff[BuffCnt+2] = 0xFF;
        SendBuff[BuffCnt+3] = 0xFF;
        break;
    }
    case SEND_PORT_DISC:
    {
        SendBuff[BuffCnt] = 0xEE;
        SendBuff[BuffCnt+1] = 0xEE;
        SendBuff[BuffCnt+2] = 0xEE;
        SendBuff[BuffCnt+3] = 0xEE;
        break;
    }
    case SEND_READ_DATA_F316:
    {
        SendBuff[BuffCnt] = 0x31;
        SendBuff[BuffCnt+1] = 0x00;
        SendBuff[BuffCnt+2] = 0x00;
        SendBuff[BuffCnt+3] = 0x00;
        break;
    }
    case SEND_BURN_DATA_F316:
    {
        SendBuff[BuffCnt] = 0x32;
        SendBuff[BuffCnt+1] = 0x00;
        SendBuff[BuffCnt+2] = 0x00;
        SendBuff[BuffCnt+3] = 0x00;
        break;
    }
//    case SEND_DATA_REPT:
    default:
    {
        SendBuff[BuffCnt] = 0x00;
        SendBuff[BuffCnt+1] = 0x0A;
        SendBuff[BuffCnt+2] = 0x00;
        SendBuff[BuffCnt+3] = 0x00;
        break;
    }
    }
    BuffCnt += 4;
    //Length
    uint16_t Size;
    Size = LineStr.size();
    SendBuff[BuffCnt] = (Size >> 8) & 0x00FF;
    SendBuff[BuffCnt+1] = Size & 0x00FF;
    BuffCnt += 2;
    //check add
    uint16_t AddVal;
    AddVal = crc16_result(SendBuff, 4);
    BuffCnt += 2;
    // 数据
    int icnt;
    if(!LineStr.isEmpty())
    {
        for(icnt=0;icnt<LineStr.size();icnt++) {
            SendBuff[BuffCnt+icnt] = LineBuff.at(icnt);
        }
        BuffCnt += icnt;
        AddVal += crc16_result(SendBuff.mid(12,Size), Size);
    }
    SendBuff[10] = (AddVal >> 8) & 0x00FF;
    SendBuff[11] = AddVal & 0x00FF;
    int StrLen = 512 - LineStr.size();
    if(StrLen>0)
    {
        RandNums.resize(StrLen);
        RandNums.clear();
        generateRandomNumber(RandNums, StrLen);

        for(icnt=0;icnt<StrLen;icnt++) {
            SendBuff[BuffCnt+icnt] = RandNums.at(icnt);
        }
        // 20230210——1711 同步时，选择语言并下发给设备，避免默认进入中文界面
        if(SEND_DATA_SYNC == SendStu)
        {
            if(LangureStu == 1)
            {
                SendBuff[38] = 0x5A;
            }
            else
            {
                if(0x5A == SendBuff[38])
                {
                    SendBuff[38] = 0xA5;
                }
            }
        }
    }

    if(SendBuff.size() != PORT_SIZE_MAX_F210)
    {
        return;
    }

    SendData_f316 = SendBuff;
    Port_f316->write(SendBuff, PORT_SIZE_MAX_F210);
}

//重发指令
void MainWindow::Port_f316_Send_Repeat()
{
    QByteArray LineStr;
    LineStr.clear();
    Port_f316_Send_Data(LineStr, PORT_STU_REPT);
}

//数据接收:触发中断->延时后接收数据
void MainWindow::Port_f316_Read_clicked()
{
    RecvTimer_f316->start(TIMER_RECV_TIME_OUT);
}
void MainWindow::Port_f316_Read_Data()
{
    RecvTimer_f316->stop();
    RecvStu = false;
    QByteArray ReadBuff;
    //从接收缓冲区中读取数据
    ReadBuff = Port_f316->readAll();
    RecvData_f316.append(ReadBuff);
    QByteArray TmpBuff;
    if(RecvData_f316.size() >= PORT_SIZE_MAX_F210)
    {
        TmpBuff.clear();
        TmpBuff.append(RecvData_f316.left(PORT_SIZE_MAX_F210));
        ReadBuff.resize(PORT_SIZE_MAX_F210);
        ReadBuff.clear();
        ReadBuff = TmpBuff;

        //校验CRC，若不成功则发送重复请求
        uint16_t AddVal;
        uint16_t AddCheckVal;
        AddVal = crc16_result(ReadBuff.mid(0,4), 4);
        //取长度
        AddCheckVal = (((ReadBuff[9]&0x00FF)<<8) & 0xFF00) | (ReadBuff[8]&0x00FF);
        int len = AddCheckVal;
        if(0 != len)
        {
            if(PORT_SIZE_MAX_F210 < len)
            {
                RecvStu = true;
                RecvData_f316.clear();
                return;
            }
            AddVal += crc16_result(ReadBuff.mid(12,len), len);
        }
        AddCheckVal = ((ReadBuff[11]<<8) & 0xFF00) | (ReadBuff[10]&0x00FF);
        if(AddVal != AddCheckVal)
        {
            RecvStu = true;
            RecvData_f316.clear();
        }
        else
        {
            uint32_t CmdId;
            CmdId = ((ReadBuff[7] << 24)&0xFF000000) | ((ReadBuff[6] << 16)&0x00FF0000) | ((ReadBuff[5] << 8)&0x0000FF00) | ((ReadBuff[4] << 0)&0x000000FF);
            if(0xE0000000 == CmdId)
            {
                RecvData_f316 = ReadBuff.mid(12,len);
                if(len>0)
                {
                    LangureStr = tr("失败,%1.").arg(ByteArray2QString(RecvData_f316,len));
                }
                else
                {
                    if(LangureStu == 1)
                    {
                        LangureStr = "Error,unknown error!";
                    }
                    else
                    {
                        LangureStr = "失败,未知错误!";
                    }
                }
                PortStu_f316 = PORT_STU_ERROR;
            }
            else if(ReadBuff[7].operator==(0x00))
            {
                if(0x000A0000 == CmdId) {
                    Port_f316->write(SendData_f316, PORT_SIZE_MAX_F210);
                    WaitTimer_f316->stop();
                    PortStu_f316 = PORT_STU_WAIT;
                    WaitTimer_f316->start(TIMER_SYNC_TIME_OUT);
                }
                else if(0x000B0000 == CmdId)
                {
                    if(WaitTimer->isActive())
                    {
                        WaitTimer_f316->start(TIMER_SYNC_TIME_OUT);
                        SendCnt_f316 = 0;
                    }
                    else if(SyncTimer->isActive())
                    {
                        SyncTimer_f316->start(TIMER_SYNC_TIME_OUT);
                        TimeOutCnt_f316 = 0;
                    }
                    PortStu_f316 = PORT_STU_TIMEOUT;
                }
            }
            else if(ReadBuff[7].operator==(0xBB))
            {
                //同步：1.端口连接后，检测数据是否为同步，同步后设置为同步状态（仅可进行界面操作）
                //校验：2.检查当前路径下是否有版本文件，若没有则报错 无法校验，请将版本文件存放到程序目录
                //校验完成：3.同步后，mcu发送当前版本号给pc，若pc校验mcu版本落后，则升级版本，此时pc设置为校验通过状态（可进行功能性操作）
                if(CmdId == 0xBB000000)
                {
                    if(PORT_STU_SYNC == PortStu_f316)
                    {
                        //取版本
                        QString ReadData = ByteArray2QString(ReadBuff, 524);
                        DevVer_f316.resize(4);
                        DevVer_f316.clear();

                        //Year
                        TmpBuff[0] = ReadData.mid(19,1).toInt();
                        TmpBuff[1] = ReadData.mid(20,1).toInt();
                        TmpBuff[2] = ReadData.mid(21,1).toInt();
                        TmpBuff[3] = ReadData.mid(22,1).toInt();
                        DevVerDate.append((TmpBuff[0]<<4|TmpBuff[1]));
                        DevVerDate.append((TmpBuff[2]<<4|TmpBuff[3]));

                        //Month,一月Jan，二月Feb，三月Mar，四月Apr，五月May，六月Jun，七月Jul，八月Aug，九月Sept，十月Oct，十一月Nov，十二月Dec
                        bool ContainStu;
                        int icnt = 0;
                        while(icnt<12)
                        {
                            ContainStu = ReadData.contains(Months[icnt], Qt::CaseInsensitive);
                            icnt++;
                            if(ContainStu)
                            {
                                break;
                            }
                        }
                        DevVerDate.append(icnt);

                        //Data
                        TmpBuff[0] = ReadData.mid(16,1).toInt();
                        TmpBuff[1] = ReadData.mid(17,1).toInt();
                        DevVerDate.append((TmpBuff[0]<<4|TmpBuff[1]));

                        if(ReadData[38] == 'v')
                        {
                            DevVer_f316.append(ReadData.mid(38,6).toLatin1());
                        }
                        else
                        {
                            DevVer_f316.append(ReadData.mid(40,4).toLatin1());
                        }
                        if(ReadData[44] == '.' && ReadData[45] == '1' && ReadData[46] == '2')
                        {
                            VersF3xName = UPG_FILE_APP_F312;
                            DevsF3xName = "F312";
                        }
                        else
                        {
                            VersF3xName = UPG_FILE_APP_F316;
                            DevsF3xName = "F316";
                        }
                        //
                        Port_F310_Get_File_Vers();
                        //
                        if(LangureStu == 1)
                        {
                            LangureStr = "Current equipment version number: ";
                        }
                        else
                        {
                            LangureStr = "当前设备版本号：";
                        }
                        ui->lbl_ver_f316->setText(LangureStr+DevVer_f316);

                        //
                        PortStu_f316 = PORT_STU_FREE;
                        if(LangureStu == 1)
                        {
                            LangureStr = DevsF3xName + " connected";
                        }
                        else
                        {
                            LangureStr = DevsF3xName + "已连接";
                        }
                        InsertText(ui->te_prin_f316, LangureStr, 9, APP_FONT_COLOR_NORMAL);

                        //
                        if((BinVer_f316.size() - DevVer_f316.size()) <= 1)
                        {
                            if(BinVer_f316 == DevVer_f316)
                            {
                                //标记同步完成状态
                                if(LangureStu == 1)
                                {
                                    LangureStr = "You can go to the official website or contact customer service to determine whether the assistant is the latest version";
                                }
                                else
                                {
                                    LangureStr = "可前往官方网站或联系客服确定助手是否为最新版本";
                                }
                                InsertText(ui->te_prin_f316, LangureStr, 9, APP_FONT_COLOR_NORMAL);
                            }
                            else
                            {
                                if(BinVer_f316 > DevVer_f316)
                                {
                                    LangureStr_Init_DevVers_Low(ui->te_prin_f316, BinVer_f316);
                                }
                                else
                                {
                                    //pc端落后，提示升级
                                    LangureStr_Init_AppVers_Low(ui->te_prin_f316);
                                }
                            }
                        }
                        else if(BinVer_f316.size() > DevVer_f316.size())
                        {
                            //mcu版本落后
                            LangureStr_Init_DevVers_Low(ui->te_prin_f316, BinVer_f316);
                        }
                        else
                        {
                            //pc端落后，提示升级
                            LangureStr_Init_AppVers_Low(ui->te_prin_f316);
                        }
                        if(PORT_STU_NULL != PortStu_f316)
                        {
                            Port_F316_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
                            SyncTimer_f316->start(TIMER_SYNC_TIME_OUT);
                        }
                    }
                    //操作后的同步帧
                    else if(PORT_STU_WAIT == PortStu_f316)
                    {
                        if(PORT_STU_WAIT_OK == LastPortStu_f316)
                        {
                            RecvData_f316.clear();
                            return;
                        }
                        else
                        {
                            PortStu_f316 = LastPortStu_f316;
                        }
                    }
                    TimeOutCnt_f316 = 0;
                }
                else {
                    if(PORT_STU_WAIT == PortStu_f316)
                    {
                        if(3 == WaitOutCnt_f316)
                        {
                            PortStu_f316 = LastPortStu_f316;
                            WaitOutCnt_f316 = 0;
                        }
                        else
                        {
                            Port_f316_Send_Repeat();
                            WaitOutCnt_f316++;
                            RecvData_f316.clear();
                            WaitTimer_f316->stop();
                            PortStu_f316 = PORT_STU_WAIT;
                            WaitTimer_f316->start(TIMER_SYNC_TIME_OUT);
                            return;
                        }
                    }
                }
                for(int cnt=0;cnt<len;cnt++) {
                    RecvData_f316.append(ReadBuff.at(12+cnt));
                }
            }
            else
            {
                //设备接收升级数据完成
                if(CmdId == 0xC8000000)
                {
                    PortStu_f316 = LastPortStu_f316;
                }
                else if(CmdId == 0x31000000)
                {
                    PortStu_f316 = LastPortStu_f316;
                    RecvData_f316.clear();
                    for (int cnt=0;cnt<len;cnt++) {
                        RecvData_f316.append(ReadBuff.at(12+cnt));
                    }
                    return;
                }
                else if(CmdId == 0x32000000)
                {
                    PortStu_f316 = LastPortStu_f316;
                }
                else
                {
                    if(10 == WaitOutCnt_f316)
                    {
                        PortStu_f316 = LastPortStu_f316;
                        WaitOutCnt_f316 = 0;
                    }
                    else
                    {
                        WaitTimer_f316->stop();
                        Port_f316_Send_Repeat();
                        WaitOutCnt_f316++;
                        RecvData_f316.clear();
                        WaitTimer_f316->start(100);
                        return;
                    }
                }
            }
            WaitOutCnt_f316 = 0;
            RecvData_f316.clear();
        }
    }
    else if(PORT_STU_SYNC == PortStu_f316)
    {
        RecvStu = true;
        RecvData_f316.clear();
    }
}

//校验版本文件
void MainWindow::Port_F310_Get_File_Vers()
{
    QString VerFileName = FilePath + "/" + VersF3xName;
    QFile VerFile(VerFileName);
    if(!VerFile.open(QIODevice::ReadOnly))
    {
        VerFile.close();
        return;
    }
    QByteArray FileData;
    FileData.clear();
    // 200*1024
    if(VerFile.size() < 0x00032000)
    {
        if(LangureStu == 1)
        {
            LangureStr = "Upgrade file error!";
        }
        else
        {
            LangureStr = "升级文件错误!";
        }
        InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_FAILED);
        VerFile.close();
        return ;
    }
    else
    {
        VerFile.seek(0x00032000);

#ifdef UPG_FILE_NAME_APP2
        if(VersFileName == UPG_FILE_NAME_APP2)
        {
            FileData.append(VerFile.read(4));
        }
        else
        {
            FileData.append(VerFile.read(6));
        }
#else
        FileData.append(VerFile.read(6));
#endif
    }

    VerFile.close();

    BinVer_f316 = FileData;
}
